<?php
/**
 * 提成明细
 * @return
 * @author yuncran@126.com
 */

namespace app\api\listener;


use app\model\ConfigModel;
use app\model\DrawLogModel;
use app\model\UserModel;

class DrawLog
{

    /**
     * 提成明细
     * @param array $param
     * @return bool|null
     * @author yuncran@126.com
     */
    public function handle(array $param)
    {
        try {
            $config = ConfigModel::where('group', 'user')->field(['id', 'name', 'value'])->select();

            $user_one_money = 0;            # 员工直属客户升级会员购买翻译包提成比例(百分比)
            $user_recharge_money = 0;       # 员工直属客户充值提成比例(百分比)
            $user_expend_money = 0;         # 员工直属客户赠送礼物自己提成比例(百分比)
            $user_user_other_money = 0;     # 员工直属客户赠送礼物对方提成比例(百分比)

            foreach ($config as $value) {
                if ($value['name'] == 'user_one') {
                    $user_one_money = $value['value'];
                }
                if ($value['name'] == 'user_recharge') {
                    $user_recharge_money = $value['value'];
                }
                if ($value['name'] == 'user_expend') {
                    $user_expend_money = $value['value'];
                }
                if ($value['name'] == 'user_other_one') {
                    $user_user_other_money = $value['value'];
                }
            }

            #员工直属下级充值提成比例(百分比)
            if ($param['expend'] == 'recharge') {
                $money = 0;
                if ($param['merchant_id'] > 0) {
                    $money = bcmul($param['money'], bcdiv($user_recharge_money, '100', 2), 2);
                }
                if ($money > 0) {
                    $formatTime = date('Y-m-d H:i:s', time());
                    DrawLogModel::create([
                        'merchant_id' => $param['merchant_id'],
                        'user_id' => $param['user_id'],
                        'money' => $money,
                        'type' => $param['type'],
                        'describe' => self::getType($param['type']),
                        'create_time' => $formatTime,
                        'update_time' => $formatTime,
                    ]);
                }
            }

            if ($param['expend'] == 'gift') {
                $formatTime = date('Y-m-d H:i:s', time());
                $money = 0;
                if ($param['merchant_id'] > 0) {
                    $money = bcmul($param['money'], bcdiv($user_expend_money, '100', 2), 2);
                }

                if ($money > 0) {
                    DrawLogModel::create([
                        'merchant_id' => $param['merchant_id'],
                        'user_id' => $param['user_id'],
                        'money' => $money,
                        'type' => $param['type'],
                        'describe' => self::getType($param['type']),
                        'create_time' => $formatTime,
                        'update_time' => $formatTime,
                    ]);
                }

                if ($user_user_other_money > 0) {
                    if (isset($param['to_user_id'])) {
                        //判断是否是其他员工会员 员工下级直播给别人消费对方提成比例(百分比)
                        $to_user_merchant = UserModel::where('id', $param['to_user_id'])->value('merchant_id');
                        if ($to_user_merchant > 0 && $to_user_merchant != $param['merchant_id']) {
                            # 员工下级直播给别人消费对方提成比例(百分比)
                            $money = 0;
                            if ($param['merchant_id'] > 0) {
                                $money = bcmul($param['money'], bcdiv($user_user_other_money, '100', 2), 2);
                            }
                            DrawLogModel::create([
                                'merchant_id' => $to_user_merchant,
                                'user_id' => $param['user_id'],
                                'money' => $money,
                                'type' => $param['type'],
                                'describe' => self::getType($param['type']),
                                'create_time' => $formatTime,
                                'update_time' => $formatTime,
                            ]);
                        }
                    }
                }
            }

            #员工直属客户升级会员购买翻译包提成比例(百分比)
            if($param['expend'] == 'expense'){
                $money = 0;
                if ($param['merchant_id'] > 0) {
                    $money = bcmul($param['money'], bcdiv($user_one_money, '100', 2), 2);
                }
                if ($money > 0) {
                    $formatTime = date('Y-m-d H:i:s', time());
                    DrawLogModel::create([
                        'merchant_id' => $param['merchant_id'],
                        'user_id' => $param['user_id'],
                        'money' => $money,
                        'type' => $param['type'],
                        'describe' => self::getType($param['type']),
                        'create_time' => $formatTime,
                        'update_time' => $formatTime,
                    ]);
                }
            }

            return true;
        } catch (\Exception $e) {
            return echoShutDown($e->getMessage() . $e->getLine());
        }
    }

    /**
     * 查找会员顶级
     * @param $bid2
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author yuncran@126.com
     */
    public function searchtclass($bid2)
    {
        $arr1 = UserModel::getUserInfoFind(['id' => $bid2], 'id,merchant_id,pid,account,nickname');
        if ($arr1) {
            if (intval($arr1['pid']) == 0) {
                $arr2 = $arr1;
            } else {
                $arr2 = UserModel::getUserInfoFind(['id' => $arr1['pid']], 'id,merchant_id,pid,account,nickname');
            }
        }
        return $arr2;
    }


    public function getType($type)
    {
        if ($type == 1) {
            $describe = '充值收益';
        } elseif ($type == 2) {
            $describe = '送礼物收益';
        } elseif ($type == 3) {
            $describe = '送金币收益';
        } elseif ($type == 4) {
            $describe = '购买语言包收益';
        } else {
            $describe = '会员升级';
        }
        return $describe;
    }
}